package com.sandy.gorm7

import grails.gorm.transactions.Transactional
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

import java.text.SimpleDateFormat

@RestController
@RequestMapping(value = "/student")
@Transactional
class StudentController {

    @GetMapping('/init')
    Student init() {
        new Student(
                name: 'sandy',
                birthday: new SimpleDateFormat('yyyy-MM-dd').parse('1982-11-21'),
        ).save()
        new Student(
                name: 'bill',
                birthday: new SimpleDateFormat('yyyy-MM-dd').parse('1985-8-29'),
        ).save()
    }
    @GetMapping('/list')
    List<Student> list() {
        Student.list()
    }
    @GetMapping('/delete')
    void deleteLast() {
        Student.list().last().delete()
    }

    @GetMapping('/where')
    List<Student> where() {
        Student.where {
            name == 'sandy'
        }.collect()
    }
    @GetMapping('/findAll')
    List<Student> findAll() {
        Student.findAll {
            if (false) {
                name == 'sandy'
            }
        }
    }
    @GetMapping('/find')
    def find() {
        Student.find {
            if (false) {
                name == 'sandy'
            }
        }
    }
}
